package cn.wanda.bi.sunshine.platform.server.controller.statistics;

import cn.wanda.bi.sunshine.platform.server.aspect.CheckLimit;
import cn.wanda.bi.sunshine.platform.server.controller.response.common.ListResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.wanda.bi.sunshine.platform.server.aspect.AuthUser;
import cn.wanda.bi.sunshine.platform.server.aspect.Logging;
import cn.wanda.bi.sunshine.platform.server.bean.model.PageReport;
import cn.wanda.bi.sunshine.platform.server.controller.request.PageRequest;
import cn.wanda.bi.sunshine.platform.server.controller.response.common.SimpleResponse;
import cn.wanda.bi.sunshine.platform.server.service.statistics.PageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;


/**
 * Created by wangfupeng1
 */
@Api(description = "报表增删改查接口")
@RestController
@RequestMapping(value = "/v1/page", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class StatisticsPageController implements Logging {

    public static final Logger logger = LoggerFactory.getLogger(StatisticsPageController.class);

    @Autowired
    private PageService pageService;

    @AuthUser
    @ApiOperation("新增报表接口，传入参数PageRequest对象，token用户认证信息，userName用户万信，loginTime登录时间，返回对象Pagereport，PageWigetConfig")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    SimpleResponse<PageRequest> addPage(@RequestBody PageRequest pageRequest,
                                        		@ApiParam(value = "登录校验码") @RequestHeader("token") String token
    		) {
        return pageService.addPage(pageRequest);
    }

    @AuthUser
    @ApiOperation("根据PageReportId进行查询，传入参数id,token用户认证信息，userName用户万信，loginTime登录时间，返回对象Pagereport，PageWigetConfig")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    SimpleResponse<PageRequest> queryPageById(@ApiParam(value = "pageId") @PathVariable Integer id,
                                              	@ApiParam(value = "登录校验码") @RequestHeader("token") String token
    		) {
        return pageService.queryPageById(id);
    }

    @AuthUser
    @ApiOperation("根据PageReportId进行修改,传入参数PageRequest，token用户认证信息，userName用户万信，loginTime登录时间，返回对象Pagereport，PageWigetConfig")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    SimpleResponse<PageRequest> updatePage(@RequestBody PageRequest pageRequest,
                                          		@ApiParam(value = "登录校验码") @RequestHeader("token") String token
           	) {
        return pageService.updatePage(pageRequest);
    }

    @AuthUser
    @ApiOperation("删除报表，根据PageReportId进行删除，传入参数PageReport的id，token用户认证信息，userName用户万信，loginTime登录时间")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    SimpleResponse<PageReport> deletePage(@ApiParam(value = "pageId") @PathVariable Integer id,
                                  				@ApiParam(value = "登录校验码") @RequestHeader("token" ) String token
    		) {
        return pageService.deletePage(id);
    }

    @CheckLimit
    @AuthUser
    @ApiOperation("查询所有报表,支持PageName模糊查询，传入参数pageName，如果不传默认list查询，index分页页码，limit分页每页条数，token用户认证信息，userName用户万信，loginTime登录时间" +
            "返回数据对象PageReport，字段意思p_name：page名称，description：描述，create_user_id：创建者id，create_user_name：创建者名字，modify_user_id :修改者id,modify_user_name:修改者名字," +
            "page_view: 浏览量," +
            "status:发布状态," +
            "create_date:创建时间," +
            "update_time修改时间'")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    ListResponse<PageReport> queryList(@ApiParam(value = "pageName") @RequestParam(value = "pageName", required = false, defaultValue = "") String pageName,
	                                           @ApiParam(value = "page页码") @RequestParam(value = "index", required = false, defaultValue = "1") Integer index,
	                                           @ApiParam(value = "page每页条数") @RequestParam(value = "limit", required = false, defaultValue = "20") Integer limit,
	                                           @ApiParam(value = "登录校验码") @RequestHeader("token") String token
    		) {
        return pageService.queryPageName(pageName,index,limit);
    }

}
